#include "headers.h"
#include "parameters.h"
#include "functions.h"
#include "macros.h"
#include "planckmapping.h"

#include <opencv/cv.h>
#include <opencv/highgui.h>



////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int
main(int argc, char **argv)
{
	gVolumeSize = gVolumeExtent.width*gVolumeExtent.height*gVolumeExtent.depth;

	gBlockSize = dim3(gVolumeExtent.width, 1);
	gGridSize = dim3(gVolumeExtent.height, gVolumeExtent.depth);
	//printf("%d %d %d %d %d %d\n", gGridSize.x, gGridSize.y, gGridSize.z, gBlockSize.x, gBlockSize.y, gBlockSize.z)

#if 1 

	pArgc = &argc;
	pArgv = argv;


	char *ref_file = NULL;

#if defined(__linux__)
	setenv("DISPLAY", ":0", 0);
#endif

	//start logs
	printf("%s Starting...\n\n", gSDKsample);
	if (checkCmdLineFlag(argc, (const char **)argv, "file"))
	{
		getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
		gFpsLimit = gFrameCheckNumber;
	}

	if (ref_file)
	{
		// use command-line specified CUDA device, otherwise use device with highest Gflops/s
		chooseCudaDevice(argc, (const char **)argv, false);
	}
	else
	{
		// First initialize OpenGL context, so we can properly set the GL for CUDA.
		// This is necessary in order to achieve optimal performance with OpenGL/CUDA interop.
		initGL(&argc, argv);

		// use command-line specified CUDA device, otherwise use device with highest Gflops/s
		chooseCudaDevice(argc, (const char **)argv, true);
	}

	// parse arguments
	char *filename;

	if (getCmdLineArgumentString(argc, (const char **)argv, "volume", &filename))
	{
		gVolumeFilename = filename;
	}

	int n;
	if (checkCmdLineFlag(argc, (const char **)argv, "size"))
	{
		n = getCmdLineArgumentInt(argc, (const char **)argv, "size");
		gVolumeExtent.width = gVolumeExtent.height = gVolumeExtent.depth = n;
	}

	if (checkCmdLineFlag(argc, (const char **)argv, "xsize"))
	{
		n = getCmdLineArgumentInt(argc, (const char **)argv, "xsize");
		gVolumeExtent.width = n;
	}

	if (checkCmdLineFlag(argc, (const char **)argv, "ysize"))
	{
		n = getCmdLineArgumentInt(argc, (const char **)argv, "ysize");
		gVolumeExtent.height = n;
	}

	if (checkCmdLineFlag(argc, (const char **)argv, "zsize"))
	{
		n = getCmdLineArgumentInt(argc, (const char **)argv, "zsize");
		gVolumeExtent.depth = n;
	}

	sdkCreateTimer(&gTimer);


	if (ref_file)
	{
		runSingleTest(ref_file, argv[0]);
	}
	else
	{
		// This is the normal rendering path for VolumeRender
		glutDisplayFunc(display);
		glutKeyboardFunc(keyboard);
		glutMouseFunc(mouse);
		glutMotionFunc(motion);
		glutReshapeFunc(reshape);
		glutIdleFunc(idle);


#if defined (__APPLE__) || defined(MACOSX)
		atexit(cleanup);
#else
		glutCloseFunc(cleanup);
#endif

		glutMainLoop();
	}

#endif
}
